package com.guanghua.brick.db;

public class OracleSQLPagerSection implements ISQLPagerSection {

	public String transformToCountSQL(String sql) {
		return "select count(*) c from (" + sql + ")";
	}

	public String transformToPageSQL(String sql, PageInfo pager) {
		int startRow = pager.getFirstResult();
		int rows = pager.getMaxResults();
		
		//速度快但是排序时可能出错的方法
		if (pager.isOracleFastPageSQL()) {
			return "select * from (select a.*, rownum " +
					ORACLE_PAGER_ROWNUM +
					" from (" + 
					sql + 
					") a) where " +
					ORACLE_PAGER_ROWNUM  +
					" > " + 
					String.valueOf(startRow) + 
					" and " +
					ORACLE_PAGER_ROWNUM  +
					" <= " + 
					String.valueOf(startRow + rows);	
		//肯定正确，但速度较慢的方法
		} else {
			return "select * from (select a.*, rownum " +
					ORACLE_PAGER_ROWNUM +
					" from (" +
					sql + 
					") a where rownum <= " + 
					String.valueOf(startRow + rows) + 
					") where " +
					ORACLE_PAGER_ROWNUM  +
					" > " + 
					String.valueOf(startRow);

		}
	}

}
